/**
 * Description: 回收子进程资源
 * File: signal.c |Time: 2021-04-07 10:34
 */

#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

void recycle(int sig)
{
    wait(NULL);
    printf("回收子进程完成\n");

    return ;
}

int main(int argc, char *argv[])
{
    pid_t pid = fork();

    if (signal(SIGCHLD, recycle) == SIG_ERR) {
        perror("signal");
        return -1;
    }

    if (pid == 0) {
        printf("son start.\n");
        int i = 5;
        while(i--){
            printf("son working.\n");
            sleep(1);
        }
        printf("son end.\n");
    } else {

        while (1) {
            printf("I am father.\n");
            sleep(1);
        }
    }

    
    return 0;

}

